home *** CD-ROM | disk | FTP | other *** search
- Path: ts1-020.jaxnet.com!user
- From: garyg@jax.jaxnet.com (Gary M. Greenberg)
- Newsgroups: comp.lang.c
- Subject: Re: Command line arguments
- Date: Sat, 09 Mar 1996 11:33:07 -0500
- Organization: Southeast Network Services, Inc.
- Message-ID: <garyg-0903961133070001@ts1-020.jaxnet.com>
- References: <4hjmpi$cuc_003@res-hall.nwu.edu>
- NNTP-Posting-Host: ts1-020.jaxnet.com
-
- In article <4hjmpi$cuc_003@res-hall.nwu.edu>, sen@nwu.edu (Subhro K. Sen) wrote:
-
- > I want to pass a filename to a C program for reading (and or writing).
- > How do I grab this command line argument from within the program?
- >
- > Thanks!
-
- Subhro, here is some guideance ...
-
- if (you write the program yourself)
- Success;
- else
- (you'll never learn to program);
-
- The following is _NOT_ written in C but should assist you.
- Assume "X" has been '#defined' as some integer value.
-
- You will need to #include <stdio.h>
-
- You will need a pointer for reading and/or writing the file.
- e.g.:
- FILE *rfp, *wfp; /* *rfp ->> Read File Ptr., *wfp ->> Write File Ptr. */
-
- Your invocation of main should include 2 variables:
- /* argc and argv are typical names for these 2 variables */
- "int main(int argc, char **argv)"
- _OR_
- "int main(int argc, char *argv[X])"
-
- you should check that the correct number of arguments were entered:
- if(argc!=X) _OR_ if(argc<X) depending on the number of args needed.
-
- do something if the number of arguments is wrong.
-
- declare a variable to hold the character string entered on the command line:
- char *yourString[X]; or char **yourString;
-
- I prefer the latter; if used that way, you'll also need to #include <stdlib.h>
- and use malloc to allocate the needed space for the file name.
- e.g.:
- /* N == index into argv[X] for the filename */
- yourString=malloc(strlen(argv[N]));
-
- Check that the return value of malloc is _not_ NULL. In _one_ statement ...
- if((yourString=malloc(strlen(argv[N])))==NULL) {
- /* handle the error */
- }
- /* else do your thing... */
-
- If is isn't NULL, you have allocated space for yourString, and you can then
- assign the file name to yourString like:
- strcpy(yourString,argv[N]);
-
- Now, you can try to open the file using the fopen() function, and do your stuff.
- Don't forget to include lots of error checking. Write your program, and if
- you can't get it working post a minimal and complete compilable example of your
- problem. Use a good reference book such as "The C Programming Language, 2nd
- Edition" by Kernighan & Ritchie to get a handle on what you need to make this
- work. Read and study the ANSI C Library; the Library is your friend! ;p
-
- You will learn by doing; so write some code and try to make it work.
-
- When I raced bicycles, we had an expression which translates well for
- programming (with a word substitution):
-
- "there are only 2 kinds of _programmers_,
- those who have crashed && those who will."
-
- C'ya,
-
- gary /* the Sorcerer's Apprentice */
-
- Free Speech in America murdered by the Communications Decency Act.
- ((U.S. Congress && Executive) == Collective Moron) == 1
-